<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>

<div class="app">
    <section class="layout">
        <section class="main-content col-lg-12" id="applicationLeftDiv">
            <div class="content-wrap">
                <div class="wrapper" style="bottom: 50px;">
                    <ul class="nav nav-tabs">
                        <li class="active">
                            <a href="#">申请单列表</a>
                        </li>
                    </ul>
                    <section class="panel panel-default">
                        <div class="panel-body">
                            <div class="table-responsive no-border">
                                <div class="datatable-top">
                                    <div class="pull-left">
                                        <form:form id="searchForm" modelAttribute="drugProvideApplication"
                                                   onsubmit="return loadDivForm('mainCenterDiv','searchFormDict','${ctx}/drug/drugStock/index');"
                                                   method="post" class="form-inline">
                                            <div class="form-group">
                                                <label class=" control-label">供货单位：</label>
                                                <select class="form-control" id="storageOut">
                                                    <option value=""></option>
                                                </select>
                                            </div>
                                            <div class="form-group">
                                                <input id="btnSubmit" class="btn btn-primary" type="button" onclick="saveProvideApplication()" value="保存"/>
                                                <input id="addDrug" class="btn btn-primary" type="button" value="增加" onclick="addDrugList()"/>
                                            </div>
                                        </form:form>
                                    </div>
                                </div>
                                <form id="saveProvideApplication" action="${ctx}/drug/drugProvideApplication/save" method="post" class="form-inline">
                                    <table id="contentTable" class="table table-bordered table-striped mg-t datatable">
                                        <thead>
                                        <tr>
                                            <th hidden>infoId</th>
                                            <th>序号</th>
                                            <th hidden>发放单位</th>
                                            <th>药品代码</th>
                                            <th>药品名称</th>
                                            <th>规格</th>
                                            <th hidden>单位代码</th>
                                            <th>单位</th>
                                            <th>包装数量</th>
                                            <th>包装规格</th>
                                            <th hidden>包装单位代码</th>
                                            <th>包装单位</th>
                                            <th hidden>厂家ID</th>
                                            <th>生产厂家</th>
                                            <th>未发放大包装库存量</th>
                                            <th>已发放大包装库存量</th>
                                            <th>操作</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                    </table>
                                </form>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </section>
        <section class="main-content col-lg-4" style="display: none" id="applicationRightDiv">
            <div class="content-wrap" id="rigthCenterDiv">
            </div>
        </section>
    </section>
</div>

<script type="text/javascript">
    loadSupplier();
    function loadSupplier() {
        $("#storageOut").html("");
        $("#storageOut").append("<option value=''></option>");
        $.ajax({
            url: '${ctx}/drug/drugExport/getSupplierList?type=' + '',
            type: 'get',
            contentType: 'application/json',
            async: true,
            success: function (data, textStatus, jqXHR) {
                if (data.length > 0) {
                    if (typeof(data[0].supplierJc) == 'undefined') {
                        $.each(data, function (index, item) {
                            $("#storageOut").append("<option value='" + item.storageCode + "'>" + item.storageName + "</option>");
                        });
                    } else {
                        $.each(data, function (index, item) {
                            $("#storageOut").append("<option value='" + item.id + "'>" + item.supplierJc + "</option>");
                        });
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
    }

    function removeList(btn) {
        $(btn).parent().parent().remove();
    }

    //保存
    function saveProvideApplication() {
        $("#saveProvideApplication").ajaxSubmit({
            success: function (data) {
                if (data.code == 'success') {
                    toastr.success(data.data);
                    loadDiv('applicationLeftDiv', '${ctx}/drug/drugProvideApplication/index');
                } else {
                    toastr.error(data.data);
                }
            },
            error: function (data) {
                toastr.error(data.data);
            }
        });
    }

    function addDrugList() {
        var storageOutId = $('#storageOut').val();
        if(storageOutId.trim() == ''){
            toastr.error('请先选择供货单位');
            return ;
        }else{
            $("#applicationLeftDiv").attr("class", "main-content col-lg-8");
            $("#applicationRightDiv").show('normal');
            return loadDiv('rigthCenterDiv', '${ctx}/drug/drugProvideApplication/form');
        }
    }
</script>